<template>
	<div class="user-pwd">
		<!-- <el-page-header @back="goBack" content="修改密码"></el-page-header> -->
		<br /><br />
		<el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm">
			<el-form-item label="旧密码" prop="old_password">
				<el-input type="password" v-model="ruleForm.old_password" placeholder="请输入旧密码" autocomplete="off" ref="old_password"></el-input>
			</el-form-item>
			<el-form-item label="新密码" prop="password">
				<el-input type="password" v-model="ruleForm.password" placeholder="请输入新密码，长度至少为6位" autocomplete="off"></el-input>
			</el-form-item>
			<el-form-item label="确认密码" prop="password2">
				<el-input type="password" v-model="ruleForm.password2" placeholder="确认密码" autocomplete="off"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
				<el-button @click="resetForm('ruleForm')">重置</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>
<script>
	export default {
		name: "updatePwd",
		data() {
			let that = this;
			var validatePass2 = function(rule, value, callback) {
				if (value === '') {
					callback(new Error('请填写新密码'));
				}else if (value !== that.ruleForm.password) {
					callback(new Error('两次密码不一致'));
				}else {
					callback();
				}
			};
			return {
				ruleForm: {
					old_password:'',
					password:'',
					password2:''
				},
				rules: {
					old_password: [{
						required: true,
						message: "请输入旧密码",
						trigger: "blur"
					},
					{
						min: 6,
						max: 12,
						message: "密码长度为6-12位",
						trigger: "blur"
					}],
					password: [{
							required: true,
							message: "请输入新密码",
							trigger: "blur"
						},
						{
							min: 6,
							max: 12,
							message: "密码长度为6-12位",
							trigger: "blur"
						}
					],
					password2: [{
							required: true,
							validator: validatePass2,
							trigger: "blur"
						},
						{
							min: 6,
							max: 12,
							message: "密码长度为6-12位",
							trigger: "blur"
						}
					]
				}
			};
		},
  methods: {
    goBack() {
      this.$router.go(-1);
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$axios
            .put(
              "/api/consumer/pwd",
              {
                old_password: this.ruleForm.old_password,
                password: this.ruleForm.password
              }
            )
            .then(res => {
              if (res.data.code == 400) {
                this.$toast(res.data.message);
                //原密码错误，清空输入并聚焦
                this.ruleForm.old_password = '';
                this.changOldPwdfouce();
              }
              if (res.data.code == 200) {
                this.$toast("密码修改成功");
                //this.$store.commit("delUser");
                this.$cookies.remove('user');
                this.$cookies.remove('token');
                this.$store.commit("delUser");
                this.$axios.defaults.headers.common["Authorization"] = "";
                this.$router.push({
                	path: "/"
                })
              }
            })
        } else {
          this.$toast("请按要求填写密码");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    changOldPwdfouce() {
      this.$nextTick(x => {
        //正确写法
        this.$refs.old_password.focus();
      });
    }
  }
};
</script>
<style lang="less">
.user-pwd {
  .el-form {
      margin: 0 5%;
    width: 90%;
  }
}
</style>